<?php

App::uses('Security', 'Utility');

class AuthusersController extends AppController {

    var $helpers = array('Html', 'Form', 'Session');
    var $uses = array('Authuser', 'Authpermission', 'Authmenu');
    var $paginate = array('limit' => '10');

    function login() {
        $this->layout = 'login';
        if (!empty($this->data)) {
            $username = $this->data['Authuser']['username'];
            $password = Security::hash($this->data['Authuser']['password']);
            $msg = '';
            if (!empty($username) && !empty($password)) {
                $arr_user = $this->Authuser->find('first', array('conditions' => array('Authuser.username' => $username,
                        'Authuser.password' => $password)));
                if (!empty($arr_user)) {
                    if ($arr_user['Authuser']['allow'] == 1) {
                        $this->Session->write('user.id', $arr_user['Authuser']['id']);
                        $this->Session->write('user.username', $arr_user['Authuser']['username']);
                        $this->Session->write('user.name', $arr_user['Authuser']['name']);
                        $this->Session->write('user.groupid', $arr_user['Authusergroup']['id']);
                        $this->Session->write('user.groupparent', $arr_user['Authusergroup']['parent_id']);
                        $this->Session->write('user.groupname', $arr_user['Authusergroup']['name']);

                        $usergroupid = $arr_user['Authusergroup']['id'];
                        $menus_fin = array();
                        $menus = $this->Authmenu->find('all', array('conditions' => array('Authmenu.parent_id' => 0), 'order' => array('Authmenu.item'),));
                        foreach ($menus as $menu) {
                            $resourcename = $menu['Authmenu']['controller'] . '/' . $menu['Authmenu']['action'];
                            if ($this->Authpermission->checkresource($usergroupid, $resourcename) == 1) {
                                array_push($menus_fin, $menu);
                            }
                        }

                        $this->Session->write('user.menu', $menus_fin);

                        $menuitem = array();
                        foreach ($menus_fin as $menu) {
                            $parent_id = $menu['Authmenu']['id'];
                            $submenu = $this->Authmenu->find('all', array('conditions' => array('Authmenu.parent_id' => $parent_id), 'order' => array('Authmenu.item'),));
                            $menuitem1 = array();
                            foreach ($submenu as $submenu_one) {
                                $resourcename = $submenu_one['Authmenu']['controller'] . '/' . $submenu_one['Authmenu']['action'];
                                if ($this->Authpermission->checkresource($usergroupid, $resourcename) == 1) {
                                    array_push($menuitem1, $submenu_one);
                                }
                            }


                            array_push($menuitem, array('menu' => $menu['Authmenu']['name'], 'menuitem' => $menuitem1));
                        }

                        $this->Session->write('user.menuitem', $menuitem);

                        $this->redirect(array('controller' => 'authusers', 'action' => 'index'));
                    } else {
                        $msg = "Acceso denegado para este Usuario";
                    }
                } else {
                    $msg = "Usuario o Contraseña incorrecta";
                }
            } else {
                $msg = "Ingrese Usuario y Contraseña";
            }
            $this->Session->setFlash(__($msg, true), 'default', array('class' => 'ui-error'), 'bad');
        }
    }

    function logout() {
        $this->Session->delete('user.id');
        $this->Session->delete('user.username');
        $this->Session->delete('user.name');
        $this->Session->delete('user.groupname');
        $this->Session->delete('user.groupid');
        $this->Session->delete('user.groupparent');
        $this->Session->delete('user.menu');
        $this->Session->delete('user.menuitem');
        $this->Session->destroy();
        $this->redirect(array('controller' => 'authusers', 'action' => 'login'));
    }

    function index() {

        $this->set('menu', 'users');
        $this->set('submenu', 'users');
        $this->set('cakeDescription', 'User');
        $this->set('title_for_layout', 'Index');
        $textsearch = "";
        if (!empty($this->data)) {
            $textsearch = $this->data['Authuser']['textsearch'];
            $this->paginate['conditions'] = array('or' => array('Authuser.username LIKE' => '%' . $textsearch . '%',
                    'Authuser.name LIKE' => '%' . $textsearch . '%',
                    'Authusergroup.name LIKE' => '%' . $textsearch . '%'
                    ));
        } else {
            if (isset($this->params['named']['textsearch'])) {
                $textsearch = $this->params['named']['textsearch'];
                $this->paginate['conditions'] = array('or' => array('Authuser.username LIKE' => '%' . $textsearch . '%',
                        'Authuser.name LIKE' => '%' . $textsearch . '%',
                        'Authusergroup.name LIKE' => '%' . $textsearch . '%'
                        ));
            }
        }
        $this->Authuser->recursive = 0;
        $this->set('authusers', $this->paginate());
        $this->set('textsearch', $textsearch);
    }

    function view($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'users');
        $this->set('cakeDescription', 'User');
        $this->set('title_for_layout', 'View');
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authuser', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('authuser', $this->Authuser->read(null, $id));
    }

    function add() {
        $this->set('menu', 'users');
        $this->set('submenu', 'users');
        $this->set('cakeDescription', 'User');
        $this->set('title_for_layout', 'Add');
        if (!empty($this->data)) {
            $this->data['Authuser']['password'] = Security::hash($this->data['Authuser']['password']);
            $this->Authuser->create();
            if ($this->Authuser->save($this->data)) {
                $this->Session->setFlash(__('The Authuser has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authuser could not be saved. Please, try again.', true));
            }
        }
        $authusergroups = $this->Authuser->Authusergroup->find('list');
        $this->set(compact('authusergroups'));
    }

    function edit($id = null) {
        $this->set('menu', 'users');
        $this->set('submenu', 'users');
        $this->set('cakeDescription', 'User');
        $this->set('title_for_layout', 'Edit');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Authuser', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {

            $id = $this->data['Authuser']['id'];
            $newpass = $this->data['Authuser']['password'];
            $arr_user = $this->Authuser->find('first', array('conditions' => array('Authuser.id' => $id)));
            if ($arr_user['Authuser']['password'] != $newpass)
                $this->data['Authuser']['password'] = Security::hash($newpass);

            if ($this->Authuser->save($this->data)) {
                $this->Session->setFlash(__('The Authuser has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The Authuser could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Authuser->read(null, $id);
        }
        $authusergroups = $this->Authuser->Authusergroup->find('list');
        $this->set(compact('authusergroups'));
    }

    function delete($id = null) {
        Configure::write('debug', 0);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Authuser', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Authuser->delete($id)) {
            $this->Session->setFlash(__('Authuser deleted', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('not deleted', true));
            $this->redirect(array('action' => 'index'));
        }
    }

}

?>
